package com.cloudea.learn.layer;

import com.cloudea.learn.Tensor;

/**
 * Sigmoid激活函数
 * @author cloudea
 * @date 2022/01/02
 */
public class Sigmoid extends Layer {
    @Override
    protected Tensor forward(Context context, Tensor... inputs) {
        return Operator.sigmoid(inputs[0]);
    }

    @Override
    public Tensor[] backward(Context context, Tensor grid, Tensor... inputs) {
        Tensor x = inputs[0];
        Tensor gridX = new Tensor(x.getShape());
        for(int i = 0; i < x.getSize(); i++){
            double xi = x.getByLocation(i);
            double exiv = Math.exp( - xi);
            double gridOne = exiv / Math.pow (1 - exiv, 2);
            gridX.setByLocation(i, grid.getByLocation(i) *  gridOne );
        }
        return new Tensor[]{gridX};
    }
}
